package com.zjun.demo.experimentaldemo.udp.client

import android.util.Log
import com.zjun.demo.experimentaldemo.udp.call.ICallBackMsg
import io.netty.channel.ChannelHandlerContext
import io.netty.channel.SimpleChannelInboundHandler
import io.netty.channel.socket.DatagramPacket
import io.netty.util.CharsetUtil

class UdpClientHandler(val iCallBackMsg: ICallBackMsg?): SimpleChannelInboundHandler<DatagramPacket>() {

    override fun channelRead0(ctx: ChannelHandlerContext, msg: DatagramPacket) {
        val message = msg.content().toString(CharsetUtil.UTF_8)
        iCallBackMsg?.callMsg(message)
        Log.i("TAG", "channelRead0: $message")
    }

    @Deprecated("Deprecated in Java")
    @SuppressWarnings("deprecation")
    override fun exceptionCaught(ctx: ChannelHandlerContext, cause: Throwable) {
        super.exceptionCaught(ctx, cause)
        cause.printStackTrace()
        ctx.close()
    }

}